package cn.e2win.base.entity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAOHibernateImpl;


@Repository
public class SysRoleDaoImpl extends BaseDAOHibernateImpl implements SysRoleDao {
	

	@Override
	public void pagerRole(Pager pager) {
		String sql = "SELECT t.* FROM ("
				+ "  SELECT t0.*"
				//+ " , t1.titles "
				+ "  FROM sys_role t0 "
				/*
				+ "   LEFT JOIN( "
				+ "   SELECT t0.roleId, GROUP_CONCAT(t1.title SEPARATOR '|') AS titles "
				+ "   FROM sys_role_function t0, sys_function t1 "
				+ "   WHERE t0.funId = t1.id AND t1.activeStatusInt=1 "
				+ "   GROUP BY t0.roleId "
				+ "  ) t1 ON t0.id = t1.roleId "
				*/
				+ ") t WHERE 1=1 AND activeStatusInt=1 "
				+ " ORDER BY ID";
		
		try {
			pager.processSql(sql);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<SysRole> getAllRole() {
		List<SysRole> list =  (List<SysRole>)getHibernateTemplate().find("FROM SysRole WHERE activeStatusInt =1 and id >= 0");
		return list;
	}

	@Override
	public List<SysUserRole> getUserRoleListByUserId(Integer userId) {
		return (ArrayList<SysUserRole>)super.getObjectListByHql("FROM SysUserRole WHERE userId=" + userId);
	}

	@Override
	public List<SysRoleFunction> getRoleFunctionListByRoleId(Integer userId) {
		return (ArrayList<SysRoleFunction>)super.getObjectListByHql("FROM SysRoleFunction WHERE roleId=" + userId+ " AND activeStatusInt=1");
	}

	@Override
	public List<SysFunction> getFunctionListByIds(String ids) {
		return (List<SysFunction>)super.getObjectListByHql("from SysFunction where id in (" + ids + ")");
	
	}
	@Override
	public List<SysFunction> getAllFuncList() {
		return (List<SysFunction>)super.getObjectListByHql("from SysFunction where 1=1");
	
	}

	@Override
	public Integer delRoleFunctionByRoleId(Integer roleId) {
		String sql = "DELETE FROM sys_role_function WHERE roleId=" + roleId;
		return super.updateBySql(sql, null);
	}



	 
}
